const std = @import("std");
const icelib = @import("icelib.zig");
const String = icelib.String;
const testing = std.testing;

test "stringTest" {
    var str = String.init(testing.allocator);
    defer str.deinit();

    try str.setNeedSize(50);
    try str.pushBack(usize, 123);
    try str.pushBack(f32, 0.01);
    try str.pushBack([]const u8, "hello");
    try str.pushBackFormat("{s}", .{",word"});
    str.clear();

    try str.pushBackCharArray("你好");
    try testing.expect(str.equal("你好"));
    try testing.expect(str.equalIgnoreCase("你好"));

    str.clear();
    try str.pushBackCharArray("HELLO");
    try str.swapChar(1, 'e');
    try testing.expect(str.equal("HeLLO"));
    str.lower();
    try testing.expect(str.equal("hello"));

    str.clear();
    try str.pushBackCharArray(",World");
    try str.insertCharArray(0, "Hello");
    try testing.expect(str.equal("Hello,World"));
    // popfront popback
    {
        const ch = try str.popFront();
        try testing.expect(ch == 'H');
        try testing.expect(str.equal("ello,World"));
        const chstr = try str.popFrontSize(4);
        defer chstr.deinit();
        try testing.expect(chstr.equal("ello"));

        const ch2 = try str.popBack();
        try testing.expect(ch2 == 'd');
        try testing.expect(str.equal(",Worl"));
        const chstr2 = try str.popBackSize(4);
        defer chstr2.deinit();
        try testing.expect(chstr2.equal("Worl"));
        try testing.expect(str.equal(","));
        str.clear();
    }
    {
        try str.pushBackCharArray("abc中文123");
        var str2 = try str.clone();
        defer str2.deinit();
        try testing.expect(str.compareStr(str2) == .Equal);
        const ch = try str2.at(-1);
        try testing.expect(ch == '3');
        try testing.expect(try str2.at(-2) == '2');
        const pos = str.find("中");
        if (pos) |new_pos| {
            try testing.expect(new_pos == 3);
        } else {
            try testing.expect(false);
        }
        const word = try str.getWord(3);
        defer word.deinit();
        try testing.expect(word.equal("中"));

        const pos2 = str.lastFind("文");
        if (pos2) |new_pos| {
            try testing.expect(new_pos == 6);
        } else {
            try testing.expect(false);
        }

        while (!str.isEmpty()) {
            const item = try str.popFrontWord();
            defer item.deinit();
            std.debug.print("popFrontWord:{s}\n", .{item.toStr()});
        }

        try str.pushBackCharArray("abc中文123");
        while (!str.isEmpty()) {
            const item = try str.popBackWord();
            defer item.deinit();
            std.debug.print("popBackWord:{s}\n", .{item.toStr()});
        }
    }
    try str.pushBackCharArray("Hello,World");
    try str.swapChar(5, '-');
    try testing.expect(str.equal("Hello-World"));
    try str.replace("World", "Zig");
    try testing.expect(str.equal("Hello-Zig"));
    try testing.expect(str.startWith("Hello"));
    try testing.expect(str.startWithIgnoreCase("HELLO"));
    try testing.expect(str.endWith("Zig"));
    try testing.expect(str.endWithIgnoreCase("ZIG"));
    try testing.expect(str.containsCharArray("-"));

    str.clear();
    try str.pushBackCharArray("      ");
    try str.trimLeft();
    try testing.expect(str.isEmpty());
    try str.pushBackCharArray("      ");
    try str.trimRight();
    try testing.expect(str.isEmpty());
    try str.pushBackCharArray("      ");
    try str.trim();
    try testing.expect(str.isEmpty());

    try str.pushBackCharArray("a,,b,,c,,d");
    {
        const str_array = str.split(",,");
        defer icelib.freeStringArray(str_array);
        try testing.expect(str_array.items.len == 4);
        for (str_array.items) |item| {
            std.debug.print("split(\",,\"):{s}\n", .{item.toStr()});
        }
    }
    {
        const str_array = str.split(",");
        defer icelib.freeStringArray(str_array);
        try testing.expect(str_array.items.len == 7);
        for (str_array.items) |item| {
            std.debug.print("split(\",\"):{s}\n", .{item.toStr()});
        }
    }
    {
        const str_array = str.splitNum(",", 3);
        defer icelib.freeStringArray(str_array);
        try testing.expect(str_array.items.len == 3);
        for (str_array.items) |item| {
            std.debug.print("splitNum(\",\", 3):{s}\n", .{item.toStr()});
        }
    }

    const sub_str = str.subStr(2, 3);
    defer sub_str.deinit();
    try testing.expect(sub_str.equal(",b,"));
}
